package com.validate.platform.web.controller;

import com.validate.platform.web.model.ResultModel;
import com.validate.platform.web.model.ResultStatus;
import com.validate.platform.web.service.SmsValidateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Yangjiyu on 2017/8/1 .
 */
@RestController
@RequestMapping("/smsValidate")
public class SmsValidateController {

    private static final Logger log = LoggerFactory.getLogger(SmsValidateController.class);

    @Autowired
    private SmsValidateService smsValidateService;

    /**
     * 发送短信验证码并存echche;设置有效时长
     * @param mobileNo
     * @return 发送状态;
     * @throws Exception
     */
    @RequestMapping(value = "/sendValidateSms", method = RequestMethod.POST)
    public ResponseEntity<ResultModel> sendValidateSms(@RequestParam("mobileNo") String mobileNo, @RequestParam("tenant") String tenant,@RequestParam("sendType") String sendType) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("tenant",tenant);
        params.put("mobileNo",mobileNo);
        params.put("sendType",sendType);
        log.info("发送短信：tenant:" + tenant + ";data:" + params);
        try {
            return new ResponseEntity(ResultModel.ok(smsValidateService.senValidateSms(params)), HttpStatus.OK);
        } catch (IllegalArgumentException illegalArgumentException) {
            log.error("短信参数：" + params + " 参数错误:参数不符合验证规则,error:" + illegalArgumentException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, illegalArgumentException.getMessage()), HttpStatus.OK);
        } catch (NullPointerException nullPointerException) {
            log.error("短信参数:" + params + " 参数错误:存在空值,error:" + nullPointerException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, nullPointerException.getMessage()), HttpStatus.OK);
        } catch (Exception exception) {
            log.error("短信参数:" + params + "系统错误:error:" + exception.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, exception.getMessage()), HttpStatus.OK);
        }
    }

    /**
     * 根据验证码读取缓存数据进行验证;
     * @param validateCode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/validateByCode", method = RequestMethod.POST)
    public ResponseEntity<ResultModel> validateByCode(@RequestParam("mobileNo") String mobileNo,@RequestParam("validateCode") String validateCode) throws Exception {
        log.info("验证短信码：phone:" + mobileNo + ";code:" + validateCode);
        try {
            return new ResponseEntity(ResultModel.ok(smsValidateService.validateByCode(mobileNo,validateCode)), HttpStatus.OK);
        } catch (IllegalArgumentException illegalArgumentException) {
            log.error("验证码为：" + validateCode + " 参数错误:参数不符合验证规则,error:" + illegalArgumentException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, illegalArgumentException.getMessage()), HttpStatus.OK);
        } catch (NullPointerException nullPointerException) {
            log.error("验证码为:" + validateCode + " 参数错误:存在空值,error:" + nullPointerException.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.CLIENT_ERROR, nullPointerException.getMessage()), HttpStatus.OK);
        } catch (Exception exception) {
            log.error("验证码为:" + validateCode + "系统错误:error:" + exception.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, exception.getMessage()), HttpStatus.OK);
        }
    }

}